package org.ipb.controller.modul.agama;

import java.util.List;

import org.ipb.model.Agama;


import util.hibernate.DAO;

import com.opensymphony.xwork2.ActionSupport;

public class ActionJQGrid extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4151236302794994027L;
	private List<Agama> list;
	private Integer rows = 0;
	private Integer page = 0;
	private String sord;
	private String sidx;
	private String searchField;
	private String searchString;
	private String searchOper;
	private String _search;
	private Integer total = 0;
	private long records = 0;

	public String get_search() {
		return _search;
	}

	public void set_search(String _search) {
		this._search = _search;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}


	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public String getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(String searchOper) {
		this.searchOper = searchOper;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public long getRecords() {
		return records;
	}

	public void setRecords(Integer records) {
		this.records = records;
	}

	@SuppressWarnings("unchecked")
	public String execute() {

		int to = (rows * page);
		int from = to - rows;

		// Count Rows (select count(*) from custumer)
		DAO<Integer> dao = new DAO<Integer>();
		records = dao.count(Agama.class);
		dao.createCriteria(Agama.class);
		if (!get_search().equals("true")) {
			if (getSidx().isEmpty())
				list = (List<Agama>) dao.find(from, to);
			else {
				dao.addOrder(getSord(), getSidx());
				list = (List<Agama>) dao.find(from, to);
			}
			// calculate the total pages for the query
		} else {
			if (getSidx().isEmpty()) {
				dao.addFilter(getSearchOper(), getSearchField(),
						getSearchString());
				list = (List<Agama>) dao.find(from, to);
			} else {
				dao.addFilter(getSearchOper(), getSearchField(),
						getSearchString());
				dao.addOrder(getSord(), getSidx());
				list = (List<Agama>) dao.find(from, to);
			}
		}
		dao.close();
		total = (int) Math.ceil((double) records / (double) rows);

		return SUCCESS;
	}

	public String getJSON() {
		return execute();
	}

	public List<Agama> getList() {
		return list;
	}

	public void setList(List<Agama> list) {
		this.list = list;
	}
}
